﻿<?php
/**
 * DESCRIPTION:
 *  loads all utils and classes, authomatically or manual
 *  if manual, don't forget to add new files to list
 * METHODS:
 *  void auto() tryes to load required files automatically
 *  array scan($dir, $pattern) scans dir for specified files, used for recursion
 *  void manual() loads only files that pointed in list
 * ======
 * @author at8eqeq3
 * @version 0.1
 */

function auto(){
    //loading utils
    $utils = scan(dirname(__FILE__).'/../utils', '.php'); //requires full path, without last '/'
    foreach($utils as $file){
        if(!strstr($file, 'autoloader') && !strstr($file, 'error')){ //write exclusions here
            require_once($file);
        }
    }
    //and now, the classes' time
    $classes = scan(dirname(__FILE__).'/../classes', '.php'); //requires full path, without last '/'
    foreach($classes as $file){
        require_once($file);
    }
}

function scan($dir, $pattern){
    $files = array();
    $dirh = opendir($dir);
    if ($dirh) {
        while ($dir_element = readdir($dirh)) {
            if(is_dir($dir.'/'.$dir_element) && ($dir_element != '.' && $dir_element != '..' && $dir_element != '.svn')){
                $nfiles = scan($dir.'/'.$dir_element, $pattern);
                if(is_array($nfiles)){
                    $files = array_merge($files, $nfiles);
                }
            }else{
                if(strstr($dir_element, $pattern)){
                    $files[] = $dir.'/'.$dir_element;
                }
            }
        }
        unset($dir_element);
        closedir($dirh);
    }
    return $files;
}

function manual(){
    require_once('/home/localhost/muscls/utils/strconv.php');
}

//no way to use this script apart from app
global $up_and_running;
if(!isset($up_and_running) || $up_and_running == false){
    echo 'from '.__FILE__.':<br />';
    echo '<b><font size="7">TITS OR GTFO!!!</font></b>'; //better to change this string ;)
}else{
    //uncomment the appropriate method
    //manual();
    auto();
}
?>
